package util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


/**
 * @author Mark Hoffman and Mark Govea
 *         (Factory.java on which this is based is by Robert c. Duvall)
 */
public class EnvironmentFactory {

    /**
     * Number of walls in the environment.
     */
    public static final int NUMBER_WALLS = 4;

    private static final double[] INITIAL_GRAVITY = new double[] {
            Vector.ANGLE_DOWN, 1 };
    private static final double INITIAL_VISCOSITY = 0.2;

    // Default values for gravity, viscosity, and Center of Mass force
    private double[] myGravity = INITIAL_GRAVITY;
    private double myViscosity = INITIAL_VISCOSITY;
    private double[] myCenterMass = new double[] { 1, 1 };
    private double[][] myWalls = new double[NUMBER_WALLS][2];

    /**
     * @param modelFile blueprint file containing target model
     */
    public void loadEnvironment (File modelFile) {
        assignDefaultWalls();
        try {
            Scanner input = new Scanner(modelFile);
            while (input.hasNext()) {
                Scanner line = new Scanner(input.nextLine());
                if (line.hasNext()) {
                    String type = line.next();
                    if ("gravity".equals(type)) {
                        gravityCommand(line);
                    }
                    if ("viscosity".equals(type)) {
                        viscosityCommand(line);
                    }
                    if ("centermass".equals(type)) {
                        centermassCommand(line);
                    }
                    if ("wall".equals(type)) {
                        wallCommand(line);
                    }
                }
            }
            input.close();
        }
        catch (FileNotFoundException e) {
            // should not happen because File came from user selection
            e.printStackTrace();
        }
    }

    private void assignDefaultWalls () {
        for (int i = 0; i < myWalls.length; i++) {
            for (int j = 0; j < 2; j++) {
                myWalls[i][j] = 1.0;
            }
        }
    }

    private void gravityCommand (Scanner line) {
        myGravity[0] = line.nextDouble();
        myGravity[1] = line.nextDouble();
    }

    private void viscosityCommand (Scanner line) {
        myViscosity = line.nextDouble();
    }

    private void centermassCommand (Scanner line) {
        myCenterMass[0] = line.nextDouble();
        myCenterMass[1] = line.nextDouble();
    }

    private void wallCommand (Scanner line) {
        int index = line.nextInt();
        myWalls[index - 1][0] = line.nextDouble();
        myWalls[index - 1][1] = line.nextDouble();
    }

    /**
     * @return gravity from the data file
     */
    public double[] getGravity () {
        return myGravity;
    }

    /**
     * @return viscosity from the data file
     */
    public double getViscosity () {
        return myViscosity;
    }

    /**
     * @return center of mass force from the data file
     */
    public double[] getCenterMass () {
        return myCenterMass;
    }

    /**
     * @return wall forces from the data file
     */
    public double[][] getWalls () {
        return myWalls;
    }
}
